//
//  XGStatusCommentTableViewCell.swift
//  TodayNews
//
//  Created by monkey on 2020/3/14.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import YYText

class XGStatusCommentTableViewCell: UITableViewCell, XGRegisterReusableViewProtocol
{
    var statusCommentViewModel: XGStatusCommentViewModel? {
        didSet {
            if let userIcon = statusCommentViewModel?.userIcon {
                userIconImageView.kf.setImage(with: URL(string: userIcon), options: [.processor(RoundCornerImageProcessor(cornerRadius: 15,targetSize: CGSize(width: 30, height: 30)))])
            }
            verifyImageView.isHidden = statusCommentViewModel?.isUserVerified != true
            userNameLabel.text = statusCommentViewModel?.userName
            diggCountButton.setTitle(statusCommentViewModel?.diggCount, for: .normal)
            contentLabel.attributedText = statusCommentViewModel?.contentAttributeString
            createTimeLabel.text = statusCommentViewModel?.createTime
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有属性
    
    /// 用户头像
    private lazy var userIconImageView = UIImageView()
    /// 用户名
    private lazy var userNameLabel = UILabel(text: "测试", textColor: UIColor.blue, fontSize: 15, textAlignment: .left)
    /// 内容
    private lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = TodayNews_Screen_Width - 60
        return label
    }()
    /// 认证标识
    private lazy var verifyImageView: UIImageView = UIImageView(image: UIImage(named: "all_v_avatar_18x18_"))
    /// 点赞按钮
    private lazy var diggCountButton: UIButton = {
        let button = UIButton(title: "0", fontSize: 15, normalColor: UIColor.darkGray, imageName: "feed_like_24x24_")
        return button
    }()
    /// 发布时间
    private lazy var createTimeLabel = UILabel(text: "测试", textColor: UIColor.darkGray, fontSize: 13, textAlignment: .left)
}

// MARK: - 私有扩展

private extension XGStatusCommentTableViewCell
{
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        
        contentView.addSubview(userIconImageView)
        contentView.addSubview(userNameLabel)
        contentView.addSubview(verifyImageView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(diggCountButton)
        contentView.addSubview(createTimeLabel)
        
        userIconImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
        verifyImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userIconImageView.snp.right).offset(-3)
            make.centerY.equalTo(userIconImageView.snp.bottom).offset(-3)
            make.size.equalTo(CGSize(width: 10, height: 10))
        }
        diggCountButton.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView)
            make.right.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 50, height: 30))
        }
        userNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView)
            make.left.equalTo(userIconImageView.snp.right).offset(10)
            make.right.lessThanOrEqualTo(diggCountButton.snp.left).offset(-10)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView.snp.centerY).offset(20)
            make.left.equalTo(userNameLabel.snp.left)
            make.right.equalToSuperview().offset(-10)
        }
        createTimeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.left.equalTo(contentLabel.snp.left)
        }
    }
}
